//
//  RootViewController.m
//  AppFrameWork
//
//  Created by Le Anh Tai on 4/25/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "RootViewController_iPhone.h"
#import "UIConstants.h"
#import "Reachability.h"
#import "AdGADBanner.h"
#import "AdiAdBanner.h"
#import "AdsCell.h"

#define INDEX_GOLD_VIEW_CONTROLLER 0
#define INDEX_RATES_VIEW_CONTROLLER 1
//#define INDEX_CHART_VIEW_CONTROLLER 2

@interface RootViewController_iPhone ()

@end

@implementation RootViewController_iPhone

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _reopenAdInterval = 20;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self.view bringSubviewToFront:_menuButton];
    
    [self.view bringSubviewToFront:_menuViewController.view];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self setupViewControllers];
    [self setupToolBarView];
    
    _lineMiddleView = [[UIView alloc]init];
    _lineMiddleView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_lineMiddleView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupViewControllers
{
    if (_contentScrollView == nil)
    {
        _goldViewController = [[GoldPriceViewController alloc] initWithNibName:@"GoldPriceViewController" bundle:nil];
        _ratesViewController = [[RatesViewController alloc] initWithNibName:@"RatesViewController" bundle:nil];
//        _chartViewController = [[ChartViewController alloc] initWithNibName:@"ChartViewController" bundle:nil];
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _contentScrollView.delegate = self;
        [_contentScrollView setPagingEnabled:YES];
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.backgroundColor = [UIColor blackColor];
        [_contentScrollView addSubview:_goldViewController.view];
        [_contentScrollView addSubview:_ratesViewController.view];
        [self.view addSubview:_contentScrollView];
    }
}

- (void)setupToolBarView
{
    if (_toolbarView == nil)
    {
        _toolbarView = [[FlatToolBarView alloc]init];        
        _toolbarView.delegate = self;
        [self.view addSubview:_toolbarView];
        
        _goldPriceItem = [_toolbarView addToolbarItemWithTitle:_goldViewController.title icon:_goldViewController.iconImage];
        _goldPriceItem.sourceLabel.text = TEXT_SOURCE_GOLD_PRICE;
        
        _rateItem = [_toolbarView addToolbarItemWithTitle:_ratesViewController.title icon:_ratesViewController.iconImage];
        _rateItem.sourceLabel.text = TEXT_SOURCE_EXCHANGE_RATE;
    }
}

- (void)reloadLanguage
{
    [_goldPriceItem setTitle:TEXT_TITLE_GOLD_PRICE forState:UIControlStateNormal];
    [_rateItem setTitle:TEXT_TITLE_EXCHANGE_RATE forState:UIControlStateNormal];
    _goldPriceItem.sourceLabel.text = TEXT_SOURCE_GOLD_PRICE;
    _rateItem.sourceLabel.text = TEXT_SOURCE_EXCHANGE_RATE;
    
    [_goldPriceItem setNeedsLayout];
    [_rateItem setNeedsLayout];
}

- (void)layoutSubviews
{
    if (_contentScrollView == nil)
    {
        return;
    }
    
    NSInteger heightStatusiOS7 = 0;
    if (IS_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_IOS7)
    {
        heightStatusiOS7 = 20;
    }
    
    _toolbarView.frame = CGRectMake(0, heightStatusiOS7, self.view.width, TOOLBAR_HEIGHT);
    _lineMiddleView.frame = RECT(0, _toolbarView.bottom, self.view.width, 1);
    
    _menuButton.frame = RECT(0, heightStatusiOS7, 40, _toolbarView.height);
    
    _contentScrollView.frame = RECT(0, _toolbarView.bottom + _lineMiddleView.height, self.view.width, self.view.height - _toolbarView.bottom- _lineMiddleView.height - _curAds.height);
    _contentScrollView.contentSize = CGSizeMake(_contentScrollView.width * 2, _contentScrollView.height);
    _goldViewController.view.frame = RECT(0, 0, _contentScrollView.width - 1, _contentScrollView.height);
    _ratesViewController.view.frame = RECT(_goldViewController.view.right + 2, 0, _contentScrollView.width - 1, _contentScrollView.height);
    
    _curAds.frame = RECT(0, self.view.height - _curAds.height, self.view.width, _curAds.height);
    _menuViewController.view.frame = self.view.bounds;
    [_menuViewController layoutSubviews];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    [self layoutSubviews];
}

#pragma mark - toolbar delegate

- (void)flatToolBar:(FlatToolBarView *)toolBarView didSelectedAtIndex:(NSInteger)selectedIndex
{
    NSLog(@"toolbar index:%d",selectedIndex);
    [_contentScrollView setContentOffset:CGPointMake(selectedIndex*_contentScrollView.width, 0) animated:YES];
}

#pragma mark - controllerSwipeViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == scrollView.width)
    {
        [_toolbarView setSelectedIndex:1];
    }
    else if (scrollView.contentOffset.x == 0)
    {
        [_toolbarView setSelectedIndex:0];
    }
    
    [_ratesViewController removeCalloutView];
    [_goldViewController removeCalloutView];
}

- (NSInteger)numberOfViewControllers
{
    return 2;
}

@end
